from pymilvus import MilvusClient
import face_recognition
import numpy as np

client = MilvusClient(
    uri="http://localhost:19530",
    db_name="face_db"
)

image = face_recognition.load_image_file("../images/wyz2.jpg")
face_locations = face_recognition.face_locations(image)
if len(face_locations) != 1:
    print('没有或出现多张人脸！！！')
    exit()

face_encodings = face_recognition.face_encodings(image, face_locations)

query_vector = face_encodings[0]

res = client.search(
    collection_name="face_collection",
    anns_field="face_info",
    data=[query_vector],
    limit=3,
    search_params={"metric_type": "COSINE"},
    # output_fields = ["face_info"]
)

for hits in res:
    for hit in hits:
        print(hit)
        # face_info = hit['entity']['face_info']
        # if face_recognition.compare_faces(np.array(face_encodings), face_info, 0.5)[0]:
        #     print(hit['id'])
